var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
const shop = require('../../../services/shop.js');
var app = getApp();

Page({
    data: {
      chart_left:0,
      is_show:false,
      wx_code:'',
        userInfo: {},
        temp_data:{},
        hasMobile: ''
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
      //shop.setPageTitle();
        var that=this;
        console.log('--------------------------------------');
      // 获取用户信息
       /*wx.getSetting({
        success: res => 
        {
          if (res.authSetting['scope.userLocation']) 
          {
            wx.getLocation({
              type: 'wgs84',
              success(res) 
              {
                var loc = { latitude: res.latitude, longitude: res.longitude};
                wx.setStorageSync('loc',loc);
              }
            })
          }
          else 
          {
            wx.chooseLocation({
              success: function (res) {
                console.log(res, "location")
                console.log(res.name)
                console.log(res.latitude)
                console.log(res.longitude)
                //
                var loc={latitude:res.latitude,longitude:res.longitude};
                wx.setStorageSync('loc',loc);
                
              },
              fail: function () {
                // fail
              },
              complete: function () {
                // complete
              }
            })
             
          }
        }
      })
      */
     var roleInfo=wx.getStorageSync('roleInfo');
     if(roleInfo!='')
     {
       this.setData({roleInfo:roleInfo});
     }
     else
     {
      this.setData({'roleInfo.RoleName':'普通用户'});
     }
      var IsDebug=wx.getStorageSync('IsDebug');
      this.setData({IsDebug:IsDebug});
      if(IsDebug=="0")
      {
        wx.setTabBarItem({
          index: 1,
          text: '装机视频',
          iconPath: '/static/images/ic_menu_topic_nor.png',
          selectedIconPath: '/static/images/ic_menu_sort_pressed.png'
        });
      }
      console.log('--------------------------------------');
      this.get_share();
    },
    onReady: function () {
      
    },
    onShow: function () {
      var that=this;
         
      let token = wx.getStorageSync('wx');
      // 页面显示
      var userInfo = wx.getStorageSync('userInfo');
      if (userInfo) 
      {
        userInfo.RoleDate=util.shortDate(userInfo.RoleDate);
        that.setData({ userInfo: userInfo});
        that.get_charge();
      }
      else 
      {
        var openid="";
        if(token!="")
        {
          openid = token.openid;
          util.request(api.CheckOpenID, { OpenID: openid }, 'GET').then(function (res) {
            if (res.Status === 100) 
            {
              var RoleID=res.Data.RoleID;
              var ST=res.Data.ST;
              if(RoleID>0)
              {
                util.request(api.GetUserRole, { ID: RoleID }, 'GET').then(function (rs) 
                {
                  if (rs.Status === 100) 
                  {
                    wx.setStorageSync('roleInfo', rs.Data);
                  }
                });
              }

              wx.showModal({
                title: '提示',
                content:'检测到您已经注册,是否自动登录',
                success(res1) 
                {
                  if (res1.confirm) 
                  {
                    res.Data.RoleDate=util.shortDate(res.Data.RoleDate);
                    wx.setStorageSync('userInfo', res.Data);
                    that.setData({ userInfo: res.Data });
                    app.globalData.userInfo = res.Data;

                  }
                  else if (res1.cancel) 
                  {
                    wx.switchTab({ url: '/pages/index/index' });
                  }
                }
              })

            }
            else
            {
              wx.navigateTo({ url: '/pages/auth/mobile/mobile' });
            }
          });
        }
        else
        {
          wx.reLaunch({ url: '/pages/start/index' });           
        }
        

      }

        
 

    },
    onHide: function () {
        // 页面隐藏

    },
    onUnload: function () {
        // 页面关闭
    },
    goto_reginfo:function(){
      wx.showModal({
        title: '提示',
        content:'是否立即加入会员',
        success(res1) 
        {
          if (res1.confirm) 
          {
            wx.navigateTo({url: '/pages/auth/note/index'})
          }
          else if (res1.cancel) 
          {
             
          }
        }
      })
    },
    send_msg:function(){
      wx.requestSubscribeMessage({
        tmplIds: ['cXwNPR9BH1miAeJz3hBYL06zSE19i4SSq0u_eZ195N8'],
        success (res) 
        {
           wx.showToast({title: '开始接收订阅消息'});
        }
      })
    },

    send_msg2:function(){
      wx.requestSubscribeMessage({
        tmplIds: ['Mro6voIQn6DT6YRbMJRZLJZJyPyzzs2hSvcYGhuKVyg'],
        success (res) 
        {
           wx.showToast({title: '开始接收订阅消息'});
        }
      })
    },
    wx_scan:function(){

      wx.scanCode({
        success: (res) => {
          console.log("扫码结果");
          console.log(res);
          wx.showModal({content: res.result})
          //this.setData({img: res.result})
        },
        fail: (res) => {
          console.log(res);
        }
      })
  

    },

    get_share:function(){
      var that=this;
      var userInfo=wx.getStorageSync('userInfo');
      var shopid=wx.getStorageSync('ShopID');
      if(userInfo!=''&&shopid!='')
      {
          var uid=userInfo.Id;
          util.request(api.QRCode, { uid: uid,  ShopID:shopid},'GET').then(function (res) {
            if (res.Status === 100) 
            {
              that.setData({wx_code:res.Data});
            }
          });
      }
    },
    swtch_tab:function(){
      var is_show=this.data.is_show;
      this.setData({is_show:!is_show});
    },
    update_user:function(e){
      var that = this;
      var tempInfo = e.detail.userInfo;
      var userInfo = wx.getStorageSync('userInfo');
      console.log(tempInfo);
      if (userInfo != '') {
        userInfo.UserIcon = tempInfo.avatarUrl;
        userInfo.UserName = tempInfo.nickName;
        var pd = {
          OpenID: userInfo.OpenID,
          Mobile: userInfo.Mobile,
          UserName: tempInfo.nickName,
          UserIcon: tempInfo.avatarUrl
        };
        util.request(api.UpdateUserIcon, pd, 'POST').then(function (res) {
          if (res.Status === 100) 
          {
            userInfo.RoleDate=util.shortDate(userInfo.RoleDate);
            that.setData({userInfo: userInfo});
            wx.setStorageSync('userInfo', userInfo);
          } else 
          {
            wx.showModal({
              title: 'ERROR',
              content: '操作发生错误'
            });
          }
        });
      }
    },
    exitLogin: function () {
        wx.showModal({
            title: '',
            confirmColor: '#b4282d',
            content: '退出登录？',
            success: function (res) 
            {
                if (res.confirm) 
                {
                  app.globalData.userInfo={};
                    //wx.removeStorageSync('wx');
                    wx.removeStorageSync('userInfo');
                    //wx.clearStorage();
                    wx.reLaunch({url: '/pages/index/index'});
                }
            }
        })

    },
    get_charge:function(){
      var that=this;
      var userid="";
      if(that.data.userInfo!="")
      {
        userid=that.data.userInfo.Id;
      }
      else
      {
        //wx.showToast({title: '请先登录'});
        return;
      }
      util.request(api.GetUsersChargeLeft, {UserID:userid},'GET').then(function (res) {
        if (res.Status === 100) 
        {
           that.setData({chart_left:res.Data});
        }
      });
    },
   
})